/**
 * 
 */
package face.unisannio;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import com.github.mhendred.face4j.AsyncAdapter;
import com.github.mhendred.face4j.AsyncFaceClient;
import com.github.mhendred.face4j.DefaultFaceClient;
import com.github.mhendred.face4j.FaceApi;
import com.github.mhendred.face4j.FaceClient;
import com.github.mhendred.face4j.RequestListener;
import com.github.mhendred.face4j.exception.FaceClientException;
import com.github.mhendred.face4j.exception.FaceServerException;
import com.github.mhendred.face4j.model.Photo;
import com.github.mhendred.face4j.response.TrainResponse;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore.Images.Media;
import android.widget.Toast;

/**
 * @author Fabio Melillo
 *
 */
public class TrainFaceTask extends AsyncTask<String, Integer, Object> {
	
	
	protected static final String API_KEY = "0889a9494b9775f3fadd8ef7179ab671";
	protected static final String API_SEC = "4f670ea4195306cbc88a940d5d1b72d2";
	protected static final String NAMESPACE = "fabio.melillo";
	private ProgressDialog dialog;
	private FaceRecognitionActivity activity;
	private AsyncFaceClient faceClient;
	private Photo photo;
	private int pixelLimit=900;
	
	public TrainFaceTask(FaceRecognitionActivity activity) {
		this.activity = activity;
	}
	
//	protected void onPreExecute() {
//		dialog = ProgressDialog.show(activity,"","Sto riconoscendo i volti. Attendere prego...", true);
//	}

	/* (non-Javadoc)
	 * @see android.os.AsyncTask#doInBackground(Params[])
	 */
	@Override
	protected Object doInBackground(String... uid) {
			faceClient = new AsyncFaceClient(API_KEY, API_SEC);
			RequestListener listener = new AsyncAdapter() {
				@Override
				public void onDetect(Photo photo) {
				}
				
				@Override
				public void onFaceServerException(FaceServerException fse, FaceApi faceApi) {
				}
				
				@Override
				public void onTrain(TrainResponse response) {
					super.onTrain(response);
				}
				
			};
			
			faceClient.addListener(listener);
			faceClient.train(uid[0]+"@"+NAMESPACE);
			return null;
	}

	
	@Override
	protected void onPostExecute(Object result) {
		super.onPostExecute(result);
		Toast toast=Toast.makeText(activity, "Inviata", Toast.LENGTH_SHORT);
//		dialog.dismiss();
//		activity.setPhoto(result);
//		activity.tagFaces();
	}
}
